/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
// JMinion.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

#include "MinionLib_External.h"
#include "ssi_scriptapi.h"
#include "phantomjvm.h"

BOOL APIENTRY DllMain( HINSTANCE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
	SetModuleHandle(hModule);
  return TRUE;
}

LRESULT WINAPI GetMsgProc(int nCode, WPARAM IN_wParam, LPARAM IN_lParam)
{
	// OK... so I would like to avoid to have to deal with this on each instance of the
	// minionlib.  Should MinionManager be global?  -OR-  Implement a GetMsgProc, with
	// the data type matching handled by another function.  A global MinionManager will
	// exist that will handle the data type synchronization, and must be accessed by the
	// custom GetMsgProc, which will then handle the MINION_DOFUNCTION command.
	if(!IN_lParam || nCode < 0 || IN_wParam == PM_NOREMOVE){
		return CallNextHookEx(GetGlobalHook(), nCode, IN_wParam, IN_lParam);
	}else{
		printf("JM: MESSAGE RECEIVED\n");
		MSG* aMsg = (MSG*)IN_lParam;
		if(aMsg->message == WM_COMMAND){
			WPARAM wParam;
			LPARAM lParam;
			wParam = aMsg->wParam;
			lParam = aMsg->lParam;

			MinionLog("WM_COMMAND RECIEVED");
			if(wParam == MINION_DOFUNCTION){
				if(!SetMinionMsgFlags(FLG_FUNC_TYPE)){
					return 0;
				}
				MinionLog("ENTERED FUNCTION");
				GetGlobalManager()->MakeMap();
				// THIS IS A CUSTOM FUNCTION ---------------------------------------
				SSI_FunctionAPI theAPI;
				String ClassData;
				String ExtraData;
				if(!ReadAPI(theAPI, ClassData, ExtraData, GetGlobalManager())){

					SetMinionSync(1);
					// Set a returned error
					return 0;
				}

				MinionLog("API Recieved");

				HWND theWin;
				theWin = (HWND)lParam;
  			CPhantomJVM JVM;
				JVM.SetExtraData(ExtraData);
				JVM.FunctionProcessor(theAPI);

				// Write Return
				GetGlobalManager()->ResetMap();
				GetGlobalManager()->WriteFunctionAPI(theAPI);
				GetGlobalManager()->ClearMap();
 				SetMinionSync(1);

				return 0;
//			}else if(wParam == MINION_GETRETURN){  // This message has not been invented yet
				// Will send multiple requests until command completed?
				// SO, shouldnt get here until command complete and can then
				// write the return data.

				// Therefore, the API must be global?
				// Perhaps attach it to the GlobalManager?
				//GetGlobalManager()->ResetMap();
				//GetGlobalManager()->WriteFunctionAPI(theAPI);
				//GetGlobalManager()->ClearMap();


			}else{
				MinionLog("PROCESSING DATA");
				return MinionMsgProc(nCode, wParam, lParam);
			}
		}else{
			return CallNextHookEx(GetGlobalHook(), nCode, IN_wParam, IN_lParam);
		}
	}
	return CallNextHookEx(GetGlobalHook(), nCode, IN_wParam, IN_lParam);
}

// This function is declared in the lib
MINIONLIBAPI HOOKPROC GetCustomProc(){
	return GetMsgProc;
}
